﻿using NPOI.HSSF.UserModel;
using OMG.Fill.Mulberry.Model.Ent;
using OMG.Fill.Mulberry.Service;
using ProjectShare;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Web;
using System.Web.Http;
namespace OMG.Fill.Mulberry.Model.UI.Controllers
{
    public class MulQuartProducController : ApiController
    {
        MulQuartProducServices mulQuartProducServices = new MulQuartProducServices();

        //[ApiActionAuthorize("获取表2季度生产情况表T_MulQuartProduc分页列表对象操作")]
        //[ApiActionException]
        public Pager<List<MulQuartProducEntity>> PostPageList(MulQuartProducQuery query)
        {
            return mulQuartProducServices.GetPageList(query);
        }
        //[ApiActionAuthorize("新增表2季度生产情况表T_MulQuartProduc指定对象操作")]
        //[ApiActionException]
        public ResResult<int> Post(MulQuartProducEntity model) {
            return mulQuartProducServices.AddInfo(model);
        }
        public ResResult<List<MulQuartStatsModel>> PostStatsList(MulNowModel model)
        {
            return mulQuartProducServices.GetStatsList(model);
        }
        public ResResult<List<MulQuartStatsModel>> PostStatsDetailList(MulNowModel model)
        {
            return mulQuartProducServices.GetDetailStats(model);
        }
        //[ApiActionAuthorize("修改表2季度生产情况表T_MulQuartProduc指定对象操作")]
        //[ApiActionException]
        public ResResult<int> Put(MulQuartProducEntity model)
        {
            return mulQuartProducServices.UpdateInfo(model);
        }
        //[ApiActionAuthorize("删除表2季度生产情况表T_MulQuartProduc指定对象操作")]
        //[ApiActionException]
        public ResResult<int> Delete(string id)
        {
            return mulQuartProducServices.DeleteInfo(id);
        }
        //[ApiActionAuthorize("获取表2季度生产情况表T_MulQuartProduc指定对象操作")]
        //[ApiActionException]
        public ResResult<MulQuartProducEntity> Get(string id)
        {
            return mulQuartProducServices.GetModel(id);
        }
        public ResResult<List<MulQuartStatsModel>> PostStatsTimeList(MulNowModel model)
        {
            return mulQuartProducServices.GetStatsByTimeList(model);
        }
        /// <summary>
        /// 导出数据
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public MemoryStream Method(string ReportId, string ReportName)
        {
            FileStream file = null;
            //创建下载Excel文件
            var workbook = new HSSFWorkbook();
            string fileIndex = HttpRuntime.AppDomainAppPath.ToString();
            string filename = @"tempfile\Basic" + DateTime.Now.Ticks.ToString() + ".xls";//临时文件，定期在tempfile手动删除
            string tempExcel = fileIndex + filename;//临时文件路径及全称
            string templateName = fileIndex + @"Excel\Excel.xls";//模板文件
            file = new FileStream(templateName, FileMode.Open, FileAccess.Read);//读模板文件
            HSSFWorkbook templateBook = new HSSFWorkbook(file);//读取模板文件信息转换成Excel工作薄
            HSSFSheet sheet = templateBook.GetSheet("MulQuartProduc") as HSSFSheet;//读取模板所在sheet位置
            MulQuartProducEntity pageModel = mulQuartProducServices.GetModel(ReportId).model;//数据获取

            //设置新标题
            HSSFRow row = (HSSFRow)sheet.GetRow(0);
            var style = row.GetCell(0).CellStyle;
            string strtitle = row.GetCell(0).StringCellValue;
            row.CreateCell(0).SetCellValue(ReportName);
            row.GetCell(0).CellStyle = style;
            HSSFRow datarow = (HSSFRow)sheet.GetRow(2);
            datarow.CreateCell(0).SetCellValue(Common.GetInt(pageModel.SeedSize));
            datarow.CreateCell(1).SetCellValue(Common.GetOne(pageModel.FreshCocoonYield));
            datarow.CreateCell(2).SetCellValue(Common.GetOne(pageModel.AveFreshCocoon));
            datarow.CreateCell(3).SetCellValue(Common.GetInt(pageModel.SmallRearSilk));
            datarow.CreateCell(4).SetCellValue(Common.GetInt(pageModel.GridCluster));
            datarow.CreateCell(5).SetCellValue(Common.GetInt(pageModel.NextSeaOutPut));
            datarow.CreateCell(6).SetCellValue(Common.GetOne(pageModel.NextSeaFresh));

            sheet.CopyTo(workbook, "Sheet1", true, true);
            using (MemoryStream ms = new MemoryStream())
            {
                workbook.Write(ms);
                ms.Flush();
                ms.Position = 0;
                return ms;
            }
        }
        /// <summary>
        /// 导出下载响应
        /// </summary>
        /// <param name="ReportCode"></param>
        public void GetExcelData(string ReportId,string ReportName)
        {

            //打开完成excel文件并下载
            HttpContext curContext = HttpContext.Current;
            // 设置编码和附件格式
            curContext.Response.AddHeader("Access-Control-Allow-Origin", "*");
            curContext.Response.ContentType = "application/vnd.ms-excel";
            curContext.Response.ContentEncoding = Encoding.UTF8;
            curContext.Response.Charset = "";
            string strFileName = ReportName + ".xls";
            curContext.Response.AppendHeader("Content-Disposition",
                "attachment;filename=" + HttpUtility.UrlEncode(strFileName, Encoding.UTF8));
            curContext.Response.BinaryWrite(Method(ReportId, ReportName).GetBuffer());
            curContext.Response.End();
        }

        public void GetStatsExcelData(string FillInYear, string AreaCode, string Status,string TimesCycle)
        {
            MulNowModel model = new MulNowModel();
            model.FillInYear = FillInYear;
            model.AreaCode = AreaCode;
            model.Status = Status;
            model.TimesCycle = TimesCycle;
            //string ProvinceCode = "530000000000";
            //AreaServices area = new AreaServices();
            //List<MulQuartStatsModel> list = new List<MulQuartStatsModel>();
            //MulQuartStatsModel m = new MulQuartStatsModel();
            //if (!string.IsNullOrEmpty(model.AreaCode))
            //{
            //    if (model.AreaCode == ProvinceCode)
            //    {
            //        var First = mulQuartProducServices.GetStatsList(model).model;
            //        foreach (var s1 in First)
            //        {
            //            MulNowModel model2 = new MulNowModel();
            //            model2.FillInYear = FillInYear;
            //            model2.AreaCode = s1.AreaCode;
            //            model2.Status = Status;
            //            var Second = mulQuartProducServices.GetStatsList(model2).model;
            //            list.Add(s1);
            //            foreach (var s2 in Second)
            //            {
            //                if (s2.AreaCode != model2.AreaCode)
            //                {
            //                    list.Add(s2);
            //                }
            //            }
            //        }
            //    }
            //    else
            //    {
            //        list = mulQuartProducServices.GetStatsList(model).model;

            //    }
            //}

            //string AreaName = area.GetByCodeModel(AreaCode).model.Name;
            ////打开完成excel文件并下载
            HttpContext curContext = HttpContext.Current;
            // 设置编码和附件格式
            curContext.Response.AddHeader("Access-Control-Allow-Origin", "*");
            curContext.Response.ContentType = "application/vnd.ms-excel";
            curContext.Response.ContentEncoding = Encoding.UTF8;
            curContext.Response.Charset = "";
            string strFileName = FillInYear +"季度生产情况表.xls";
            curContext.Response.AppendHeader("Content-Disposition",
                "attachment;filename=" + HttpUtility.UrlEncode(strFileName, Encoding.UTF8));
            curContext.Response.BinaryWrite(mulQuartProducServices. MethodStats(model).GetBuffer());
            curContext.Response.End();
        }
    }
}